package com.example.trb.controller.error;

import com.example.trb.util.result.CommonResult;
import com.example.trb.util.result.SysReturnCode;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
public class NotFoundException implements ErrorController {
    @Autowired
    ObjectMapper objectMapper;

    public static String getErrorPath() {
        return "/error";
    }

    @RequestMapping(value = "/error")
    @ResponseBody
    public Object error(HttpServletRequest request) throws JsonProcessingException {
        SysReturnCode code = SysReturnCode.NO_FOUND;
        return objectMapper.writeValueAsString(
                CommonResult.fail(
                        code.code,
                        code.message
                )
        );
    }
}